Setting Up

Please follow these steps before or soon after the first lecture.

Software

Install the following software. If you already have R/RStudio installed, please re-install both.

  • R programming language and software environment for statistical computing and graphics
  • RStudio (preview version) open source integrated development environment (IDE) for R.
  • LaTeX word processor and document markup language: + TeXWorks for Mac and Windows or + MacTeX for Mac (2.5GB) + MiKTeX for Windows (176MB)
  • On Macs, when prompted to install command line developper tools, select “Install”
  • On Windows, you should get a similar prompt.

Installing Packages

We now describe how to install R packages, or extensions to R, from the CRAN repository of packages.

  • In one of the panels in RStudio, there is a tab Packages.
  • Click Install and in the Packages field type ggplot2 dplyr to install both those packages.
  • If prompted to restart R, say yes.
  • In another panel, there is a tab Console. Type library(ggplot2) and library(dplyr) and ensure the resulting messages does not contain any errors.

RStudio

RStudio is an integrated development environment that acts as a user interface for R.

In R Studio there are (by default) four “panes” to work in.

These can all be configured, but we’ll stick with the default configuration for now.

R studio Screenshot

R studio Screenshot


Source Pane

The Source Pane provides several helpful features:

  • Highlighting
  • Line Numbering
  • Indentation and Parentheses

Console Pane

The console “is” R. You feed commands into it, and it produces output. It also keeps track of variables, functions, etc. that you define, and the previous commands you’ve given.

The console pane is where you directly interact with the console. You can do this by typing commands in directly, or by running them from the source pane.

The output from the console is often given in the console. Plots appear separately (by default in the “Plots/Help” pane).


Workspace

You can view a few different things in the upper right. By default you’ll see the Environment, where R shows you what things it’s keeping track of (Data frames, variables, functions). Many of these things, e.g. data frames and functions, can be opened in the source pane for viewing.

You can also view the commands the console has run, in the order it ran them.


Plots/Help/Packages

Plots is self-explanatory. Note that you can flip back and forth between various plots you’ve made.

The Help browser allows you to search for documentation on functions you are using, or might use. You can also access these from the console. Great when you know roughly what you want to do, but need to check syntax. Also, check the “See Also” at the bottom if you’re not sure what function you’re looking for.

You can install packages from the Packages tab by clicking Install and the name of the package you are trying to install. _Note: Packages must be loaded via library() command to be able to use the package. for eg.

library(cars)

Useful RStudio Keyboard Shortcuts

  1. Tab: complete command and variable names
  2. (From console) Up: scroll thru previous commands
  3. (From editor) CMD+Enter or Control+Enter: execute in console either current line or highlighted code

To see them all, press alt+shift+k.

  1. It is good practice to change your working directory in R to the folder you want to read files from and write files to. This can be accomplished in RStudio in two different ways:

    • In the Files menu/window (Ctrl+5) click “More” (gears image), and then “Set As Working Directory”
    • In the R console (Ctrl+2) type: setwd(“~/Rtuts”)
    • On Mac, use Session menu and then “Set Working Directory”

Notice that if you chose to perform the first option above, RStudio automatically types the second option out for you in the console.

You can find out what your working directory is at any point by typing

getwd()

in the R console.

R Markdown

In RMarkdown there are three different content:

R Markdown

R Markdown

In-class exercise: Testing R Markdown

  1. Open RStudio and starting in the menu bar, go to File -> New File -> R Markdown…
  2. If prompted to install any packages, say yes.
  3. A document Untitled1 should pop-up.
  4. Give it an arbitrary title
  5. Change summary(cars) in the first code block to print("Hello world!")
  6. In that panel, click on Knit HTML or Knit PDF.
  7. Give the file a name and save your Rmd file as helloworld.Rmd

An HTML/PDF document should pop-up in the Viewer tab.

Resources